package com.morgankelly.dao;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.morgankelly.po.Account;
import com.morgankelly.util.PMF;

public class AccountDAO extends GenericDAO<Account> {
	
	private static AccountDAO instance = null;

	private AccountDAO() {}

	public static synchronized AccountDAO get() {
		if (instance == null) {instance = new AccountDAO();}
		return instance;
	}

	@SuppressWarnings("unchecked")
	public Account getAccountByName(String name){

		PersistenceManager pm = PMF.get().getPersistenceManager();
		
		Query query = pm.newQuery(Account.class, "name == accountName");
		query.declareParameters("String accountName");

		List<Account> accountList = (List<Account>) query.execute(name);
		
		if (accountList != null && !accountList.isEmpty()) {
			return  accountList.get(0);
		} else {
			return  null;
		}
	}
	
	public void update(Long id, String name, String password,
			String displayName, String email, String role, String picPath) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Account entry = pm.getObjectById(Account.class, id);

		entry.setName(name);
		entry.setPassword(password);
		entry.setDisplayName(displayName);
		entry.setEmail(email);
		entry.setRole(role);
		entry.setPicPath(picPath);

		pm.makePersistent(entry);

	}
	
	
}
